#include "ringBuffer.h"
#include<stdlib.h>


ringBuffer_s inputRingBuffer = {
	NULL,
	0,
	0,
	0
};

BOOL initializeRingBuffer(ringBuffer_p this,int size)
{
	
	this->buffer=(char*)malloc(size*sizeof(char));
	if(this->buffer==NULL)
	{
		return FALSE;
	}
	this->bufferSize=size;
	
	return TRUE;
}

void destroyRingBuffer(ringBuffer_p this)
{
	free(this->buffer);
}


BUFF_RC writeToRingBuffer(ringBuffer_p this,char* input)
{
	int loc=(this->upperLimit+1)%this->bufferSize;
	if( loc== this->lowerLimit)
	{
		return LOWERLIMIT;
	}
	this->buffer[loc]=*input;
	this->upperLimit=loc;
	return OK;
}

BUFF_RC readFromRingBuffer(ringBuffer_p this,char* output)
{
    int loc=(this->lowerLimit+1)%this->bufferSize;
	if(loc==this->upperLimit)
	{
		return UPPERLIMIT;
	}
	*output=this->buffer[loc];
	this->lowerLimit=loc;
	
	return OK;
}
